#include <iostream>
#include <clocale>
#include "List.h"
#include "Node.h"
#include <string>
#include <fstream>
#include "..\Course\hr_time.h"
using namespace std;
/** \brief Contains separate functions for separate textfiles, reading the files and creating lists of them. 
* The functions also measure the time used to read the files.
*
*/

int main_test()
{
	List<int> list;

	int f = 10;

	if (list.search(f) == nullptr)
	{
		cout << "search: tom lista - nullptr" << endl;
	}


	list.add_at_head(20);
	cout << list.str() << endl;
	f = 20;
	if (list.search(f) == nullptr)
	{
		cout << "search: finns ej i listan - nullptr" << endl;
	}
	else
	{
		cout << "search: " << f << endl;
	}

	list.add_at_head(30);
	cout << list.str() << endl;
	f = 20;
	if (list.search(f) == nullptr)
	{
		cout << "search: finns ej i listan - " << f << endl;
	}
	else
	{
		cout << "search: " << f << endl;
	}

	list.add_at_head(40);
	list.add_at_head(50);
	list.add_at_head(60);
	cout << list.str() << endl;
	f = 50;
	list.remove(list.search(f));
	cout << list.str() << endl;

	list.add_at_head(70);
	list.add_at_head(80);
	cout << "Test of iterator: ";
	for (auto it = list.begin(); it != list.end(); it++)
	{
		cout << (*it).info << " ";
	}

	return 0;
}

int tal50000main()
{
	CStopWatch watch;
	double time1, time2;
	List <int> iList, evenList, oddList;

	fstream ifs;

	ifs.open("tal50000.txt");

	int tal;
	watch.startTimer();
	while (ifs.good())
	{
		ifs >> tal;
		//cout << tal << " ";
		if (iList.search(tal) == nullptr)
		{
			iList.add_at_head(tal);
		}

	}
	watch.stopTimer();
	time1 = watch.getElapsedTime();
	time1 *= 1000;
	/*cout << "Time elapsed: "<< time1 << "ms" << endl;*/
	ifs.close();


	ifs.open("tal50000.txt");
	watch.startTimer();
	while (ifs.good())
	{
		ifs >> tal;

		if (tal % 2 == 0)
		{
			if (evenList.search(tal) == nullptr)
				evenList.add_at_head(tal);
		}
		else
		{
			if (oddList.search(tal) == nullptr)
				oddList.add_at_head(tal);
		}
	}
	watch.stopTimer();
	time2 = watch.getElapsedTime();
	time2 *= 1000;
	/*cout << "Time elapsed: " << time2  << "ms"<< endl;*/
	ifs.close();
	/*cout << iList.str() << endl;*/
	cout << endl << ">>>> tal50000.txt <<<<" << endl;
	cout << "Time elapsed, complete list: " << time1 << "ms" << endl;
	/*iList.split_odd_even(oddList, evenList);*/
	/*cout << evenList.str() << endl;
	cout << oddList.str() << endl;*/
	cout << "Time elapsed, separated lists: " << time2 << "ms" << endl;

	return 0;
}

int tal10000main()
{
	CStopWatch watch;
	double time1, time2;
	List <int> iList, evenList, oddList;

	fstream ifs;

	ifs.open("tal10000.txt");

	int tal;
	watch.startTimer();
	while (ifs.good())
	{
		ifs >> tal;
		//cout << tal << " ";
		if (iList.search(tal) == nullptr)
		{
			iList.add_at_head(tal);
		}

	}
	watch.stopTimer();
	time1 = watch.getElapsedTime();
	time1 *= 1000;
	/*cout << "Time elapsed: "<< time1 << "ms" << endl;*/
	ifs.close();


	ifs.open("tal10000.txt");
	watch.startTimer();
	while (ifs.good())
	{
		ifs >> tal;

		if (tal % 2 == 0)
		{
			if (evenList.search(tal) == nullptr)
				evenList.add_at_head(tal);
		}
		else
		{
			if (oddList.search(tal) == nullptr)
				oddList.add_at_head(tal);
		}
	}
	watch.stopTimer();
	time2 = watch.getElapsedTime();
	time2 *= 1000;
	/*cout << "Time elapsed: " << time2  << "ms"<< endl;*/
	ifs.close();
	/*cout << iList.str() << endl;*/
	cout << endl << ">>>> tal10000.txt <<<<" << endl;
	cout << "Time elapsed, complete list: " << time1 << "ms" << endl;
	/*iList.split_odd_even(oddList, evenList);*/
	//cout << evenList.str() << endl;
	//cout << oddList.str() << endl;
	cout << "Time elapsed, separated lists: " << time2 << "ms" << endl;

	return 0;
}

int tal1000main()
{
	CStopWatch watch;
	double time1, time2;
	List <int> iList, evenList, oddList;

	fstream ifs;

	ifs.open("tal1000.txt");

	int tal;
	watch.startTimer();
	while (ifs.good())
	{
		ifs >> tal;
		//cout << tal << " ";
		if (iList.search(tal) == nullptr)
		{
			iList.add_at_head(tal);
		}

	}
	watch.stopTimer();
	time1 = watch.getElapsedTime();
	time1 *= 1000;
	/*cout << "Time elapsed: "<< time1 << "ms" << endl;*/
	ifs.close();


	ifs.open("tal1000.txt");
	watch.startTimer();
	while (ifs.good())
	{
		ifs >> tal;

		if (tal % 2 == 0)
		{
			if (evenList.search(tal) == nullptr)
				evenList.add_at_head(tal);
		}
		else
		{
			if (oddList.search(tal) == nullptr)
				oddList.add_at_head(tal);
		}
	}
	watch.stopTimer();
	time2 = watch.getElapsedTime();
	time2 *= 1000;
	/*cout << "Time elapsed: " << time2  << "ms"<< endl;*/
	ifs.close();
	/*cout << iList.str() << endl;*/
	cout << endl << ">>>> tal1000.txt <<<<" << endl;
	cout << "Time elapsed, complete list: " << time1 << "ms" << endl;
	/*iList.split_odd_even(oddList, evenList);*/
	/*cout << evenList.str() << endl;
	cout << oddList.str() << endl;*/
	cout << "Time elapsed, separated lists: " << time2 << "ms" << endl;

	return 0;
}

int exempelmain()
{
	CStopWatch watch;
	double time1, time2;
	List <int> iList, evenList, oddList;

	fstream ifs;

	ifs.open("exempelfil.txt");

	int tal;
	watch.startTimer();
	while (ifs.good())
	{
		ifs >> tal;
		//cout << tal << " ";
		if (iList.search(tal) == nullptr)
		{
			iList.add_at_head(tal);
		}

	}
	watch.stopTimer();
	time1 = watch.getElapsedTime();
	time1 *= 1000;
	/*cout << "Time elapsed: "<< time1 << "ms" << endl;*/
	ifs.close();


	ifs.open("exempelfil.txt");
	watch.startTimer();
	while (ifs.good())
	{
		ifs >> tal;

		if (tal % 2 == 0)
		{
			if (evenList.search(tal) == nullptr)
				evenList.add_at_head(tal);
		}
		else
		{
			if (oddList.search(tal) == nullptr)
				oddList.add_at_head(tal);
		}
	}
	watch.stopTimer();
	time2 = watch.getElapsedTime();
	time2 *= 1000;
	/*cout << "Time elapsed: " << time2  << "ms"<< endl;*/
	ifs.close();
	cout << ">>>> exempelfil.txt <<<<" << endl;
	/*cout << iList.str() << endl;*/
	cout << "Time elapsed, complete list: " << time1 << "ms" << endl;
	/*iList.split_odd_even(oddList, evenList);*/
	/*cout << evenList.str() << endl;
	cout << oddList.str() << endl;*/
	cout << "Time elapsed, separated lists: " << time2 << "ms" << endl;

	return 0;
}

int main()
{
	setlocale(LC_ALL, "swedish");

	exempelmain();
	tal1000main();
	tal10000main();
	tal50000main();

	system("PAUSE");
	return 0;
}